/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Entities.Users;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import sessionBeans.loginSessionBean;

/**
 *
 * @author xespin
 */
@ManagedBean(name="loginenter")
@SessionScoped
public class loginManagedBean {
    @EJB
    private loginSessionBean loginsess;
    private String password;
    private String login;
    private String userVisit;
    private Long userId;
    

    /** Creates a new instance of loginManagedBean */
    public loginManagedBean() {
        
    }
    
    /**
     * Checks if a user login is valid
     * @return 
     */
    public String checkLogin(){
        Users user = null;
        if(this.password != null && this.login != null){
            user = loginsess.getUser(this.login, this.password);
        }
       
        Users visitor = null;
        if(this.userVisit != null){
           visitor = loginsess.getUser(this.userVisit);
        }
        
        if(visitor != null){
            return "visit";
        }

        if(user != null){
            this.userId = user.getId();
            return "main";
        }
        
        return "register";
    }
    
    /**
     * Allows to close the session to start a new one
     * @return 
     */
    public String closeSession(){
        loginsess.terminate();
        this.login = "";
        this.password = "";
        return "index";
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getLogin() {
        return login;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setUserVisit(String userVisit) {
        this.userVisit = userVisit;
    }

    public String getUserVisit() {
        return userVisit;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }    
}
